
package com.es.controller;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.es.business.ILoginManagement;
import com.es.business.LoginManagementImpl;
import com.es.exception.business.ESAuthenticationException;
import com.es.model.SalesAgent;
import com.es.model.SalesWorkForce;
import com.es.util.ProjectConstant;

/**
 * @author Bushra Nazir
 *
 */
public class AuthenticationController extends BaseController{

	private static final Logger log = Logger.getLogger(AuthenticationController.class);

	private ILoginManagement loginManagementImpl;

	private String userCode;
	private String password;



	public String login() throws Exception {

		log.info("inside login()");

		SalesWorkForce salesWorkForceObj = null;

		//SalesAgent salesAgent = null;



		FacesContext fc = FacesContext.getCurrentInstance();
		HttpServletRequest request = getRequestFromFacesContext();
		String navigationIdentifier = null;

		try {
			if (loginManagementImpl==null){

				log.info("loginManagementImpl is null");
				loginManagementImpl=new LoginManagementImpl();
			}

			salesWorkForceObj = loginManagementImpl.login(userCode, password );



			registerUserSession(salesWorkForceObj);
			
			// make User user having ref to SalesWorkForce ad corresponding subtype and store that in session
			

			if (salesWorkForceObj.getType().equals(ProjectConstant.SALES_AGENT_TYPE_CD)) {

				navigationIdentifier = ProjectConstant.SALES_AGENT_DASHBOARD_NAVIGATION_IDENTIFIER;

			} else if (salesWorkForceObj.getType().equals(ProjectConstant.SALES_MANAGER_TYPE_CD)) {

				navigationIdentifier = ProjectConstant.SALES_MANAGER_DASHBOARD_NAVIGATION_IDENTIFIER;

			}

		} catch (ESAuthenticationException authenticationException) {
			log.error("Error: " + authenticationException.getMessage());

			String errMsg = getMessageResourceString(
					ProjectConstant.APPLICATION_MESSAGE_RESOURCES_PATH,
					"LOGIN_INVALID_USERCODE_PASSWORD_ERR", null, request
					.getLocale());
			addGlobalFacesMessage(fc, errMsg, null,
					FacesMessage.SEVERITY_ERROR);


			return null;
		} 


		return navigationIdentifier;
	}



	private void registerUserSession(SalesWorkForce salesWorkForceObj) {

		HttpServletRequest request = getRequestFromFacesContext();
		request.getSession().setAttribute(ProjectConstant.USER_SESSION_KEY,salesWorkForceObj);
	}


	public String logout() {

		String navigationIdentifier = ProjectConstant.LOGIN_PAGE_NAVIGATION_IDENTIFIER;

		try{


			HttpSession session = getSessionFromFacesContext();
			SalesWorkForce swfUser = null;

			if(session != null){

				session.invalidate();
				return navigationIdentifier;
			}
		
	}catch (Exception e) {
		log.error(e);
		e.printStackTrace();
		navigationIdentifier = ProjectConstant.LOGIN_PAGE_NAVIGATION_IDENTIFIER;
	}

	return navigationIdentifier;

}







public String getUserCode() {
	return userCode;
}



public void setUserCode(String userCode) {
	this.userCode = userCode;
}



public String getPassword() {
	return password;
}



public void setPassword(String password) {
	this.password = password;
}



public ILoginManagement getLoginManagementImpl() {
	System.out.println("inside getter");
	return loginManagementImpl;
}



public void setLoginManagementImpl(ILoginManagement loginManagementImpl) {
	System.out.println("inside setter param="+loginManagementImpl);

	this.loginManagementImpl = loginManagementImpl;
}




}
